import axios from 'axios'
import store from '@/store'
import Vue from 'vue'
import cookie from '@/util/cookie'
import router from '@/router'
// import {refreshToken} from '@/api/commonApi'
// import { Encrypt } from '@/util/crypto'
// console.log(axios)
axios.defaults.timeout = 0
axios.defaults.withCredentials = false // 跨域请求，允许保存cookie
// console.log(axios.defaults)
// 添加请求拦截器
let loading
axios.interceptors.request.use(function (config) {
  config.headers['token'] = cookie.get('token')
  config.headers['X-Requested-With'] = 'XMLHttpRequest'
 
  loading =  Vue.prototype.$toast.loading({
    message: '加载中...',
    forbidClick: true,
    type:'loading',
    duration:0
  })

  return config

}, function (error) {
  loading.clear()
  console.log('error' + error)
  return Promise.reject(error)
})
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  loading.clear()
 
  return response
}, function (error) {
  loading.clear()
  if (error.response.status == 401) {
    Vue.prototype.$dialog.alert({
        message: '登录已过期，请重新登录',
      }).then(() => {
        router.push('login')
      })
      return
  }
  return Promise.reject(error.response.data.error.message)
})
export default axios
